import { defineStore } from 'pinia';

export default defineStore({
  id: 'user',
  state: () => {
    return {
      token: uni.getStorageSync('token') || '',
      userInfo: uni.getStorageSync('userInfo') || {},
      roles: uni.getStorageSync('roles') || [],
      permissions: uni.getStorageSync('permissions') || []
    };
  },
  actions: {
    logOutApp() {
      this.token = '';
      this.userInfo = {};
      this.permissions = [];
      this.roles = [];
    },
    setToken(token) {
      this.token = token ? `Bearer ${token}` : '';
      uni.setStorageSync('token', this.token);
    },
    setUserInfo(userInfo) {
      this.userInfo = userInfo;
      uni.setStorageSync('userInfo', userInfo);
    },
    setRoles(roles) {
      this.roles = roles;
      uni.setStorageSync('roles', roles);
    },
    setPermissions(data) {
      this.permissions = data;
      uni.setStorageSync('permissions', data);
    },
    async getUserInfo() {
      // 获取用户信息
      // const res = await getInfo();
      // this.setUserInfo(res.user);
      // this.setRoles(res.roles);
      // this.setPermissions(res.permissions);
    }
  }
});
